const path = require('path')
const utils = require('./build/modulesutil')

// 用于做相应的merge处理
const merge = require('webpack-merge')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

const productionMode = process.env.NODE_ENV === 'production'

const resolve = dir => {
  return path.join(__dirname, dir)
}

let buildModule = process.argv[3];
let publicPath = buildModule === undefined ? '/' : '/' + buildModule;

module.exports = {
  publicPath: publicPath,
  productionSourceMap: false,
  pages: utils.pages(),

  chainWebpack: config => {
    config.module
      .rule('images')
      .use('url-loader')
      .tap(options =>
        merge(options, {
          limit: 5120,
        })
      )

    config.resolve.alias
      .set('@', resolve('src'))
  },

  configureWebpack: config => {

    if (productionMode) {
      return {
        plugins: [
          new CompressionWebpackPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
            threshold: 10240,
            minRatio: 0.8
          }),
          // new BundleAnalyzerPlugin()
        ]
      }
    }
  },

  devServer: {
    open: true,
    host: '127.0.0.1',
    port: 9000,
    https: false,
    progress: true,
  }
}
